package com.yayo.mangement.conf;

import com.google.common.collect.Maps;
import com.yayo.mangement.authentication.enums.GenerateLayerEnum;
import org.springframework.util.ClassUtils;

import java.util.List;
import java.util.Map;

public class PackageFileConfig {
    private static final String CONTROLLER = "yayo-rest/src/main/java/com/yayo/mangement/controller/system/";
    private static final String SERVICE = "yayo-server/src/main/java/com/yayo/mangement/service/";
    private static final String SERVICE_IMPL ="yayo-server/src/main/java/com/yayo/mangement/service/impl/";
    private static final String DAO = "yayo-server/src/main/java/com/yayo/mangement/dao/";
    private static final String DTO = "yayo-server/src/main/java/com/yayo/mangement/dto/";
    private static final String MODEL = "yayo-server/src/main/java/com/yayo/mangement/model/";
    private static final String MAPPER = "yayo-server/src/main/resources/mapper/";
    private static final String FACADE = "yayo-server/src/main/java/com/yayo/mangement/facade/";
    private static final String FACADE_IMPL ="yayo-server/src/main/java/com/yayo/mangement/facade/impl/";
    private static final String CONVERTER ="yayo-server/src/main/java/com/yayo/mangement/converter/";

    public static final String PACKAGE_NAME = "com.yayo.mangement";

    private static final Map<String, String> initLayerMap;

    static{
        initLayerMap = Maps.newHashMap();
        initLayerMap.put("Controller.java", getRealPath() + CONTROLLER);
        initLayerMap.put("Facade.java", getRealPath() + FACADE);
        initLayerMap.put("FacadeImpl.java", getRealPath() + FACADE_IMPL);
        initLayerMap.put("Service.java", getRealPath() + SERVICE);
        initLayerMap.put("ServiceImpl.java", getRealPath() + SERVICE_IMPL);
        initLayerMap.put("Dao.java", getRealPath() + DAO);
        initLayerMap.put("DTO.java", getRealPath() + DTO);
        initLayerMap.put("DO.java", getRealPath() + MODEL);
        initLayerMap.put("Converter.java", getRealPath() + CONVERTER);
        initLayerMap.put("Mapper.xml", getRealPath() + MAPPER);
    }


    /**
     * 初始化配置文件代码位置集合
     */
    public static Map<String, String> getCodePathList(List<String> layerList){
        Map<String, String> result = Maps.newHashMap();
        layerList.stream().forEach(layer -> {
            if(GenerateLayerEnum.CONTROLLER.getLayer().equals(layer)){
                result.put("Controller.java", getRealPath() + CONTROLLER);
            }else if(GenerateLayerEnum.FACADE.getLayer().equals(layer)){
                result.put("Facade.java", getRealPath() + FACADE);
                result.put("FacadeImpl.java", getRealPath() + FACADE_IMPL);
            }else if(GenerateLayerEnum.SERVICE.getLayer().equals(layer)){
                result.put("Service.java", getRealPath() + SERVICE);
                result.put("ServiceImpl.java", getRealPath() + SERVICE_IMPL);
            }else if(GenerateLayerEnum.DAO.getLayer().equals(layer)){
                result.put("Dao.java", getRealPath() + DAO);
            }else if(GenerateLayerEnum.DTO_DO_CONVERTER.getLayer().equals(layer)){
                result.put("DTO.java", getRealPath() + DTO);
                result.put("DO.java", getRealPath() + MODEL);
                result.put("Converter.java", getRealPath() + CONVERTER);
            }else if(GenerateLayerEnum.MAPPER.getLayer().equals(layer)){
                result.put("Mapper.xml", getRealPath() + MAPPER);
            }
        });
        return result;
    }

    /**
     * 获取生成地址绝对路径
     * @return
     */
    private static String getRealPath(){
        String realPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        realPath = realPath.substring(0,realPath.indexOf("yayo"));
        return realPath;
    }

}
